/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.common.mime.shim.JavaMailInternetAddress;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.common.zmime.ZMimeBodyPart;
/*     */ import com.zimbra.common.zmime.ZMimeMessage;
/*     */ import com.zimbra.common.zmime.ZMimeMultipart;
/*     */ import com.zimbra.cs.util.JMSession;
/*     */ import java.io.ByteArrayOutputStream;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintStream;
/*     */ import java.util.Date;
/*     */ import javax.activation.DataHandler;
/*     */ import javax.mail.Message.RecipientType;
/*     */ import javax.mail.MessagingException;
/*     */ import javax.mail.internet.MimeBodyPart;
/*     */ import javax.mail.internet.MimeMessage;
/*     */ import javax.mail.internet.MimeMultipart;
/*     */ import javax.mail.util.ByteArrayDataSource;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class MessageBuilder
/*     */ {
/*     */   private String subject;
/*     */   private String from;
/*     */   private String sender;
/*     */   private String toRecipient;
/*     */   private String ccRecipient;
/*     */   private String body;
/*     */   private Date date;
/*     */   private String contentType;
/*     */   private Object attachment;
/*     */   private String attachmentFilename;
/*     */   private String attachmentContentType;
/*     */   private boolean addMessageIdHeader;
/*     */   
/*  53 */   public MessageBuilder() { this.addMessageIdHeader = false; }
/*     */   
/*  55 */   static String DEFAULT_MESSAGE_BODY = "Dude,\r\n\r\nAll I need are some tasty waves, a cool buzz, and I'm fine.\r\n\r\nJeff";
/*     */   
/*     */ 
/*     */   private class MimeMessageWithNoId
/*     */     extends ZMimeMessage
/*     */   {
/*     */     MimeMessageWithNoId()
/*     */     {
/*  63 */       super();
/*     */     }
/*     */     
/*     */     protected void updateMessageID() throws MessagingException
/*     */     {
/*  68 */       removeHeader("Message-ID");
/*     */     }
/*     */   }
/*     */   
/*     */   public MessageBuilder withSubject(String subject) {
/*  73 */     this.subject = subject;
/*  74 */     return this;
/*     */   }
/*     */   
/*     */   public MessageBuilder withFrom(String from) {
/*  78 */     this.from = from;
/*  79 */     return this;
/*     */   }
/*     */   
/*     */   public MessageBuilder withSender(String address) {
/*  83 */     this.sender = address;
/*  84 */     return this;
/*     */   }
/*     */   
/*     */   public MessageBuilder withToRecipient(String recipient) {
/*  88 */     this.toRecipient = recipient;
/*  89 */     return this;
/*     */   }
/*     */   
/*     */   public MessageBuilder withCcRecipient(String recipient) {
/*  93 */     this.ccRecipient = recipient;
/*  94 */     return this;
/*     */   }
/*     */   
/*     */   public MessageBuilder withBody(String body) {
/*  98 */     this.body = body;
/*  99 */     return this;
/*     */   }
/*     */   
/*     */   public MessageBuilder withDate(Date date) {
/* 103 */     this.date = date;
/* 104 */     return this;
/*     */   }
/*     */   
/*     */   public MessageBuilder withContentType(String contentType) {
/* 108 */     this.contentType = contentType;
/* 109 */     return this;
/*     */   }
/*     */   
/*     */   public MessageBuilder withAttachment(Object content, String filename, String contentType) {
/* 113 */     if (content == null) {
/* 114 */       throw new IllegalArgumentException("content cannot be null");
/*     */     }
/* 116 */     if (StringUtil.isNullOrEmpty(contentType)) {
/* 117 */       throw new IllegalArgumentException("contentType cannot be null or empty");
/*     */     }
/* 119 */     if (StringUtil.isNullOrEmpty(filename)) {
/* 120 */       throw new IllegalArgumentException("filename cannot be null or empty");
/*     */     }
/* 122 */     this.attachment = content;
/* 123 */     this.attachmentFilename = filename;
/* 124 */     this.attachmentContentType = contentType;
/* 125 */     return this;
/*     */   }
/*     */   
/*     */   public MessageBuilder withMessageIdHeader() {
/* 129 */     this.addMessageIdHeader = true;
/* 130 */     return this;
/*     */   }
/*     */   
/*     */   public String create() throws MessagingException, ServiceException, IOException {
/* 134 */     if (this.toRecipient == null) {
/* 135 */       this.toRecipient = "user1";
/*     */     }
/* 137 */     if (this.from == null) {
/* 138 */       this.from = "jspiccoli";
/*     */     }
/* 140 */     if (this.date == null) {
/* 141 */       this.date = new Date();
/*     */     }
/* 143 */     if (this.contentType == null) {
/* 144 */       this.contentType = "text/plain";
/*     */     }
/* 146 */     if (this.body == null) {
/* 147 */       this.body = DEFAULT_MESSAGE_BODY;
/*     */     }
/* 149 */     this.from = TestUtil.addDomainIfNecessary(this.from);
/* 150 */     this.toRecipient = TestUtil.addDomainIfNecessary(this.toRecipient);
/* 151 */     this.sender = TestUtil.addDomainIfNecessary(this.sender);
/*     */     
/* 153 */     MimeMessage msg = this.addMessageIdHeader ? new ZMimeMessage(JMSession.getSession()) : new MimeMessageWithNoId();
/*     */     
/* 155 */     msg.setRecipient(Message.RecipientType.TO, new JavaMailInternetAddress(this.toRecipient));
/* 156 */     if (this.ccRecipient != null) {
/* 157 */       this.ccRecipient = TestUtil.addDomainIfNecessary(this.ccRecipient);
/* 158 */       msg.setRecipient(Message.RecipientType.CC, new JavaMailInternetAddress(this.ccRecipient));
/*     */     }
/* 160 */     msg.setFrom(new JavaMailInternetAddress(this.from));
/* 161 */     if (this.sender != null) {
/* 162 */       msg.setSender(new JavaMailInternetAddress(this.sender));
/*     */     }
/* 164 */     msg.setSentDate(this.date);
/* 165 */     msg.setSubject(this.subject);
/*     */     
/* 167 */     if (this.attachment == null)
/*     */     {
/*     */ 
/* 170 */       msg.setDataHandler(new DataHandler(new ByteArrayDataSource(this.body.getBytes(), this.contentType)));
/*     */     } else {
/* 172 */       MimeMultipart multi = new ZMimeMultipart("mixed");
/* 173 */       MimeBodyPart body = new ZMimeBodyPart();
/* 174 */       body.setDataHandler(new DataHandler(new ByteArrayDataSource(this.body.getBytes(), this.contentType)));
/* 175 */       multi.addBodyPart(body);
/*     */       
/* 177 */       MimeBodyPart attachment = new ZMimeBodyPart();
/* 178 */       attachment.setContent(this.attachment, this.attachmentContentType);
/* 179 */       attachment.setHeader("Content-Disposition", "attachment; filename=" + this.attachmentFilename);
/* 180 */       multi.addBodyPart(attachment);
/*     */       
/* 182 */       msg.setContent(multi);
/*     */     }
/*     */     
/* 185 */     ByteArrayOutputStream out = new ByteArrayOutputStream();
/* 186 */     msg.writeTo(out);
/* 187 */     return new String(out.toByteArray());
/*     */   }
/*     */   
/*     */   public static void main(String[] args) throws Exception {
/* 191 */     TestUtil.cliSetup();
/* 192 */     System.out.println(new MessageBuilder().withSubject("attachment test").withAttachment("attachment", "test.txt", "text/plain").create());
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/MessageBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */